#include "sylar/http/http_parser.h"
#include "sylar/log.h"

static sylar::Logger::ptr g_logger = SYLAR_LOG_ROOT();

static char test_request_data[] = "POST / HTTP/1.1\r\n"
                                  "Host: www.baidu.com\r\n"
                                  "Content-Length: 10\r\n\r\n"
                                  "1234567890";

void test_request() {
    sylar::http::HttpRequestParser parser;
    std::string tmp = test_request_data;
    size_t s = parser.execute(&tmp[0], tmp.size());
    SYLAR_LOG_INFO(g_logger) << "execute rt=" << s 
        << " has_error=" << parser.hasError()
        << " is_finished=" << parser.isFinished()
        << " total=" << tmp.size()
        << " content-length=" << parser.getContentLength();

    tmp.resize(tmp.size() - s);

    SYLAR_LOG_INFO(g_logger) << parser.getData()->toString();
	SYLAR_LOG_INFO(g_logger) << tmp;
}

const char test_response_data[] = "HTTP/1.0 200 OK\r\n"
								  "Accept-Ranges: bytes\r\n"
								  "Cache-Control: no-cache\r\n"
								  "Content-Length: 14615\r\n"
								  "Content-Type: text/html\r\n"
								  "Date: Sat, 23 Jan 2021 10:04:44 GMU\r\n"
								  "Pragma: no-cache\r\n\r\n"
								  "<html>\r\n"
										"<meta http-equiv=\"refresh\" content=\"url=http://www.baidu.com/\"\r\n"
								  "</html>\r\n";

void test_response() {
   	sylar::http::HttpResponseParser parser;
	std::string tmp = test_response_data;
	size_t s = parser.execute(&tmp[0], tmp.size(), true);
	SYLAR_LOG_ERROR(g_logger) << "execute rt= " << s
		<< " has error=" << parser.hasError() 
		<< " is_finished=" << parser.isFinished()
	    << " total=" << tmp.size()
		<< " content-length=" << parser.getContentLength()
		<< " tmp[s]=" << tmp[s];
	
	tmp.resize(tmp.size() - s);

    SYLAR_LOG_INFO(g_logger) << parser.getData()->toString();
	SYLAR_LOG_INFO(g_logger) << tmp;
}	

int main(int argc, char** argv) {
    test_request();
	
	SYLAR_LOG_INFO(g_logger) << "---------------";
    test_response();
    return 0;
}

